import { Commit } from 'vuex';
import Cookie from 'js-cookie';
const state: any = {
  isLogined: false,
	userInfo: {},
};


const mutations: any = {
  saveLoginState(states: any, params: object) {
    states.isLogined = params;
  },
  saveUserInfo(states: any, params: object) {
    states.userInfo = params;
  },
  logout(states: any, params: object) {
    states.userInfo = {};
    states.isLogined = false;
    Cookie.remove('p_token', {
      expires: 1,
      path: 'user'
    });
    Cookie.remove('p_userType', {
      expires: 1,
      path: 'user'
    });
    Cookie.remove('p_username', {
      expires: 1,
      path: 'user'
    });
    Cookie.remove('p_userId', {
      expires: 1,
      path: 'user'
    });
    window.location.hash = '/Home/Missions';
  }
};

const actions: any = {
  saveLoginStateFN(context: { commit: Commit }, params: object) {
    context.commit('saveLoginState', params);
  },
  saveUserInfoFN(context: { commit: Commit }, params: object) {
    context.commit('saveUserInfo', params);
  },
  logoutFN(context: { commit: Commit }, params: object) {
    context.commit('logout', params);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};